<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" type="text" @click="hAdd(2,row.id)">添加</el-button>
              <el-button type="text" @click="hEsit(row.id) ">编辑</el-button>
              <el-button type="text" @click="hDel(row.id) ">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-dialog
        :title="isEsit?'编辑':'添加'"
        :visible.sync="showDialog"
        width="50%"
        @close="hclose"
      >
        <el-form ref="addform" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="hSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addPermission, delPermission, getPermissionDetail, getPermissionList, updatePermission } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'VueAdminTemplatePermissions',

  data() {
    // 编辑or添加,名称or标识
    // 1.编辑-名称:找出和同级的名称,并排除自己,过滤出来
    // 2.添加-名称:找出当前部门下的所有子部门,循环数组,看输入的名字跟数组里面的名字有没有一样的
    // 编辑-标识:排除自己,循环数组,看输入的标识跟数组里面的标识有没有一样的
    // 添加-标识:找出和自己一样的标识,找出和自己子部门一样的标识
    const nameList = (rule, value, callback) => {
      let listName = null
      // 添加名称
      listName = this.originList.filter(item => item.pid === this.formData.pid).some(item => item.name === value)
      if (this.isEsit) {
      // 编辑名称
        listName = this.originList.filter(item => item.pid === this.formData.pid && item.id !== this.formData.id).some(item => item.name === value)
      }
      listName ? callback(new Error(value + '已存在')) : callback()
    }
    const codeList = (rule, value, callback) => {
      let listCode = null
      listCode = this.originList.some(item => item.code === value)
      if (this.isEsit) {
        listCode = this.originList.filter(item => item.id !== this.formData.id).some(item => item.code === value)
      }
      listCode ? callback(new Error(value + '已存在')) : callback()
    }
    return {
      list: [],
      originList: [],
      showDialog: false,
      isEsit: false,
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: ['blur', 'change'] },
          { validator: nameList, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入标识', trigger: ['blur', 'change'] },
          { validator: codeList, trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入描述', trigger: ['blur', 'change'] },
          { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },

  created() {
    this.loadPermissionList()
  },

  methods: {
    // 获取权限列表
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log(res)
        this.list = tranListToTreeData(res.data)
        this.originList = res.data
      } catch (error) {
        console.log(error.message)
      }
    },
    async  hDel(id) {
      try {
        await this.$confirm('真的要删除吗', '提示', { type: 'warning' })
        const res = await delPermission(id)
        this.$message.success(res.message)
        this.loadPermissionList()
      } catch (e) {
        this.$message.success(e.message)
      }
    },
    async  hEsit(id) {
      this.isEsit = true
      this.showDialog = true
      try {
        console.log(id)
        const res = await getPermissionDetail(id)
        // 数据回填

        this.formData = res.data
      } catch (error) {
        console.log(error.message)
      }
    },
    hAdd(type, pid) {
      this.isEsit = false
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    async  doAdd() {
      this.isEsit = false
      try {
        const res = await addPermission(this.formData)
        this.$message.success(res.message)
        this.showDialog = false
        this.$nextTick(() => {
          this.$refs.addform.clearValidate()
        })
        this.loadPermissionList()
      } catch (error) {
        console.log(error.message)
      }
    },
    async doEsit() {
      this.isEsit = true
      // console.log(11)
      try {
        const res = await updatePermission(this.formData)
        this.$message.success(res.message)
        this.showDialog = false
        this.loadPermissionList()
      } catch (error) {
        console.log(error.message)
      }
    },
    async  hSubmit() {
      const valid = await this.$refs.addform.validate()
      if (!valid) return
      this.isEsit ? this.doEsit() : this.doAdd()
    },
    hclose() {
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      this.$refs.addform.clearValidate()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
